package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.mapper.ClassesMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/classes")
public class ClassesServlet extends HttpServlet {

    private ClassesService classesService;

    @Override
    public void init() throws ServletException {
        //在service里得到spring容器
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //得到studentService
        classesService = wac.getBean(ClassesService.class);

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Classes> classes=classesService.findAll();

        resp.getWriter().println(JSON.toJSONString(R.ok().data("list",classes)));

        resp.getWriter().flush();

    }
}